package com.homedroid.tasks;

import java.io.IOException;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.homedroid.dal.Client;
import com.homedroid.dal.Parser;
import com.homedroid.dal.Product;
import com.homedroid.dal.User;

public class ServerTask extends AsyncTask<Void, String, Boolean> {

	protected String message, password, code, barcode, label, brand, content_, content_2, unit, acknowledge;
	protected int u_id;
	protected Context context;
	protected User user;
	protected ProgressDialog dialog;
	protected boolean connected = false, result = false, success = false;
	protected Client client;
	protected Parser parser = new Parser();
	protected ArrayList<Product> templist = new ArrayList<Product>();

	protected SharedPreferences preferences;
	
	public ServerTask(Context context, String code) throws IOException{
		this.context = context;
		this.code = code;
		dialog = new ProgressDialog(context);
		client = new Client();
	}

	public ServerTask(Context context, User user) throws IOException{
		this.context = context;
		this.user = user;
		dialog = new ProgressDialog(context);
		client = new Client();
	}
	
	public ServerTask(Context context,  ArrayList<Product> templist, User user) throws IOException{
		this.context = context;
		this.templist = templist;
		this.user = user;
		dialog = new ProgressDialog(context);
		client = new Client();
	}
	
	public ServerTask(Context context, String barcode, String label, String brand, String content_, String content_2, String unit) throws IOException {
		this.barcode = barcode;
		this.label = label;
		this.brand = brand;
		this.content_ = content_;
		this.content_2 = content_2;
		this.unit = unit;
		this.context = context;
		dialog = new ProgressDialog(context);
		client = new Client();
	}

	@Override
	protected void onPreExecute() {

		this.dialog.setMessage("Connecting...");
		this.dialog.show();
		Log.i("AsyncTask", "onPreExecute");
		preferences = PreferenceManager.getDefaultSharedPreferences(context);

	}

	@Override
	protected void onProgressUpdate(String... values) {
		if (values.length > 0) {
			Log.i("AsyncTask", "onProgressUpdate: " + values[0]);
			this.dialog.setMessage(values[0]);
			if (!client.nsocket.isConnected()) {
				Toast.makeText(context, "No Connection to Server", Toast.LENGTH_SHORT).show();

			}
		}
	}

	@Override
	protected void onCancelled() {
		Log.i("AsyncTask", "Cancelled.");
	}

	@Override
	protected void onPostExecute(Boolean result) {

	}
	
	protected void parserAction(){

	}

	@Override
	protected Boolean doInBackground(Void... params) {
		return null;
	}

}
